from cold_start_analyzer.base_analyzer import BaseAnalyzer
from cold_start_analyzer.consts import NOT_AVAILABLE
from cold_start_analyzer import db_helper
from cold_start_analyzer.log_helper import dlog


class Phase4All(BaseAnalyzer):
    description = "首帧渲染: 从JsWindowStage::OnLoadContent开始到首个H:OnVsyncCallback结束"
    """
    起点： app_thread
    H:Native async work complete callback, name:JsWindowStage::OnLoadContent 开始
    终点： app_thread
    H:Native async work complete callback, name:JsWindowStage::OnLoadContent 结束
    """
    def get_result(self):
        if 'Phase3All' not in self.context:
            dlog.error(f"Phase3All not in context: {self.context}")
            return NOT_AVAILABLE
        res = db_helper.get_function_cost(self.cursor, 'H:OnVsyncCallback', self.context['Phase3All']['end_ts']-1, None, thread_name=self.thread_name)
        if not res:
            dlog.error(f"query_function_cost failed: {self.thread_name}, res: {res}")
            return NOT_AVAILABLE
        self.context['Phase4All'] = {
            'start_ts': self.context['Phase3All']['end_ts'],
            'end_ts': res[0][1],
        }
        return res[0][1] - self.context['Phase3All']['end_ts']
